package com.wondertek.liveroom;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PageItemView extends RelativeLayout {
	
	private Context context = null;
	
	private ImageView ivItem = null;
	private TextView tvItem = null;
	private JSONObject objItem = null;

	public PageItemView(Context context) {
		
		super(context);
		this.context = context;
		setupView();
	}
	
	public PageItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		setupView();
	}
	
	public PageItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		setupView();
	}
	
	private void setupView() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.item_page_view, null);
		ivItem = (ImageView) view.findViewById(R.id.iv_item_page_view);
		tvItem = (TextView) view.findViewById(R.id.tv_item_page_view_title);
		addView(view);
	}
	
	public void setItemData(JSONObject obj) {
		this.objItem = obj;
		try {
			int resId = obj.getInt("itemId");
			String strTitle = obj.getString("itemName");
			ivItem.setBackgroundResource(resId);
			tvItem.setText(strTitle);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void recycle() {
		ivItem.setImageBitmap(null);
	}
	
	public void reload() {
		setItemData(this.objItem);
	}
	
}
